package com.yanqu.road.dao.impl.config;


import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.HashMap;
import java.util.Map;

public class LanguageDaoImpl extends TempDao {

    public Map<String, Map<String, String>> getMulLanguageMap() {
        Map<String, Map<String, String>> languageMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_language";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                String key = rs.getString("key");
                String language = rs.getString("language");
                String content = rs.getString("content");
                if (!languageMap.containsKey(key)) {
                    languageMap.put(key, new HashMap<>());
                }
                languageMap.get(key).put(language, content);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return languageMap;
    }

    public Map<String, Map<String, String>> getMultipleLanguageMap() {
        Map<String, Map<String, String>> languageMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_multi_language";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            while (rs.next()) {
                String key = rs.getString("id");
                if (!languageMap.containsKey(key)) {
                    languageMap.put(key, new HashMap<>());
                }
                String language;
                for(int i = 1; i < rsmd.getColumnCount() + 1; i++){
                    language = rsmd.getColumnName(i);
                    if(!"id".equals(language)) {
                        languageMap.get(key).put(language, rs.getString(language));
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return languageMap;
    }

    public Map<String, Map<String, String>> getServerLanguageMap() {
        Map<String, Map<String, String>> languageMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_language";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                String key = rs.getString("key");
                String language = rs.getString("language");
                String content = rs.getString("content");
                if (!languageMap.containsKey(key)) {
                    languageMap.put(key, new HashMap<>());
                }
                languageMap.get(key).put(language, content);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return languageMap;
    }

    public Map<String, Map<String, String>> getServerLanguageMapChannel(int channelId) {
        Map<String, Map<String, String>> languageMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_language_channel where channelId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1,channelId);
            rs = call.executeQuery();
            while (rs.next()) {
                String key = rs.getString("key");
                String language = rs.getString("language");
                String content = rs.getString("content");
                if (!languageMap.containsKey(key)) {
                    languageMap.put(key, new HashMap<>());
                }
                languageMap.get(key).put(language, content);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return languageMap;
    }
}
